package com.linq.cool.beauty.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.linq.cool.beauty.domain.BeautyProduct;
import com.linq.cool.beauty.dto.BeautyProductDTO;
import com.linq.cool.beauty.dto.BeautyProductQueryDTO;
import com.linq.cool.beauty.dto.BeautyProductSearchDTO;
import com.linq.cool.beauty.service.BeautyProductService;
import com.linq.cool.common.constants.UserConstants;
import com.linq.cool.common.core.domain.BaseController;
import com.linq.cool.common.core.domain.PageVO;
import com.linq.cool.common.core.domain.Result;
import com.linq.cool.common.core.utils.SecurityUtils;
import com.linq.cool.common.log.annotation.Log;
import com.linq.cool.common.log.enums.BusinessType;
import com.linq.cool.common.security.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author: yqlin
 * @Date: 2021/2/5 17:26
 * @Description:
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("product")
public class BeautyProductController extends BaseController {
    @Autowired
    private BeautyProductService beautyProductService;


    @PreAuthorize(hasPerm = "beauty:product:list")
    @GetMapping("list")
    public Result<?> list(BeautyProductQueryDTO dto) {
        IPage<BeautyProduct> page = beautyProductService.listByPage(dto.getPage(), dto);
        return Result.ok(new PageVO().setTotal(page.getTotal()).setRecords(page.getRecords()));
    }

    @Log(moduleTitle = "商品管理", businessType = BusinessType.INSERT)
    @PreAuthorize(hasPerm = "beauty:product:add")
    @PostMapping
    public Result<?> add(@Validated @RequestBody BeautyProductDTO dto) {
        return this.toResult(beautyProductService.saveProduct(dto));
    }


    @Log(moduleTitle = "商品管理", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPerm = "beauty:product:edit")
    @PutMapping
    public Result<?> edit(@Validated @RequestBody BeautyProductDTO dto) {
        return this.toResult(beautyProductService.updateProduct(dto));
    }

    @GetMapping(value = "{productId}")
    public Result<?> getInfo(@PathVariable("productId") Long productId) {
        return Result.ok(beautyProductService.getProductById(productId));
    }

    @Log(moduleTitle = "商品管理", businessType = BusinessType.DELETE)
    @PreAuthorize(hasPerm = "beauty:product:remove")
    @DeleteMapping("{productIds}")
    public Result<?> remove(@PathVariable("productIds") List<Long> productIds) {
        return this.toResult(beautyProductService.removeByIds(productIds));
    }


    @Log(moduleTitle = "商品管理", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPerm = "beauty:product:edit")
    @PutMapping("changePublishStatus/{productId}/{publishStatus}")
    public Result<?> changePublishStatus(@PathVariable("productId") Long productId, @PathVariable("publishStatus") Integer publishStatus) {
        // 先查询出来
        BeautyProduct product = beautyProductService.getById(productId);
        if (null == product) {
            return Result.fail("该商品不存在");
        }
        product.setPublishStatus(publishStatus).setModifier(SecurityUtils.getUserId()).setGmtModified(new Date());
        return this.toResult(beautyProductService.updateById(product));
    }


    @Log(moduleTitle = "商品管理", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPerm = "beauty:product:edit")
    @PutMapping("changeStatus/{productId}/{status}")
    public Result<?> changeStatus(@PathVariable("productId") Long productId, @PathVariable("status") Integer status) {
        // 先查询出来
        BeautyProduct product = beautyProductService.getById(productId);
        if (null == product) {
            return Result.fail("该商品不存在");
        }
        product.setStatus(status).setModifier(SecurityUtils.getUserId()).setGmtModified(new Date());
        return this.toResult(beautyProductService.updateById(product));
    }


    @Log(moduleTitle = "商品管理", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPerm = "beauty:product:edit")
    @PutMapping("changeRecommandStatus/{productId}/{recommandStatus}")
    public Result<?> changeRecommandStatus(@PathVariable("productId") Long productId, @PathVariable("recommandStatus") Integer recommandStatus) {
        // 先查询出来
        BeautyProduct product = beautyProductService.getById(productId);
        if (null == product) {
            return Result.fail("该商品不存在");
        }
        product.setRecommendStatus(recommandStatus).setModifier(SecurityUtils.getUserId()).setGmtModified(new Date());
        return this.toResult(beautyProductService.updateById(product));
    }


    @Log(moduleTitle = "商品管理", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPerm = "beauty:product:edit")
    @PutMapping("changeVerifyStatus/{productId}/{verifyStatus}")
    public Result<?> changeVerifyStatus(@PathVariable("productId") Long productId, @PathVariable("verifyStatus") Integer verifyStatus) {
        // 先查询出来
        BeautyProduct product = beautyProductService.getById(productId);
        if (null == product) {
            return Result.fail("该商品不存在");
        }
        product.setVerifyStatus(verifyStatus).setModifier(SecurityUtils.getUserId()).setGmtModified(new Date());
        return this.toResult(beautyProductService.updateById(product));
    }


    @Log(moduleTitle = "商品管理", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPerm = "beauty:product:edit")
    @PutMapping("changeNewStatus/{productId}/{newStatus}")
    public Result<?> changeNewStatus(@PathVariable("productId") Long productId, @PathVariable("newStatus") Integer newStatus) {
        // 先查询出来
        BeautyProduct product = beautyProductService.getById(productId);
        if (null == product) {
            return Result.fail("该商品不存在");
        }
        product.setNewStatus(newStatus).setModifier(SecurityUtils.getUserId()).setGmtModified(new Date());
        return this.toResult(beautyProductService.updateById(product));
    }

    /**
     * 获取新品
     *
     * @return
     */
    @GetMapping("getNewProducts")
    public Result<?> getNewProducts() {
        return Result.ok(beautyProductService.listByNewStatus());
    }

    /**
     * 获取推荐商品
     */
    @GetMapping("gerRecommendProducts")
    public Result<?> gerRecommendProducts() {
        return Result.ok(beautyProductService.listByRecommendStatus());
    }

    /**
     * 查询排行榜
     * 查询所在类目销量最高的6条数据
     *
     * @param productId
     *
     * @return
     */
    @GetMapping("getRankTopSixProducts/{productId}")
    public Result<?> getRankTopSixProducts(@PathVariable("productId") Long productId) {
        return Result.ok(beautyProductService.listBySale(productId));
    }

    /**
     * 商品搜索
     */
    @PostMapping("search")
    public Result<?> search(@RequestBody BeautyProductSearchDTO dto) {
        return Result.ok(beautyProductService.search(dto));
    }

    /**
     * 批量查询
     *
     * @param productIds
     *
     * @return
     */
    @GetMapping("getProducts/{productIds}")
    public Result<?> getProducts(@PathVariable("productIds") List<Long> productIds) {
        return Result.ok(beautyProductService.listByIds(productIds));
    }

    /**
     * 前台使用 查看商品详细信息 因为未登录的时候 可以查询详情
     * 在网关也需要放行
     *
     * @return
     */
    @GetMapping(value = "detail")
    public Result<?> detail(@RequestParam("productId") Long productId) {
        BeautyProduct one = beautyProductService.getOne(new LambdaQueryWrapper<BeautyProduct>()
                                                                .eq(BeautyProduct::getIsDeleted, UserConstants.StateEnum.DELETE_FLAG_EXIST.getCode())
                                                                .eq(BeautyProduct::getId, productId)
        );
        if (null == one) {
            return Result.ok("该商品已下架");
        }
        return Result.ok(one);
    }
}
